import pandas as pd
def count_shared_cubes(x, y, z):##这个函数负责把每个点被多少个正方体共用算出来
    if (x==0 and y==0 and z==0) or (x==0 and y==0 and z==z_num) or (x==x_num and y==0 and z==0) or (x==x_num and y==0 and z==z_num) or (x==x_num and y==y_num and z==0) or (x==x_num and y==y_num and z==z_num) or (x==0 and y==y_num and z==0) or (x==0 and y==y_num and z==z_num):
        return 1
    else:
        if (y==0 and z==0) or (x==0 and z==0) or (x==x_num and z==0) or (y==y_num and z==0) or (y==0 and z==z_num) or (x==0 and z==z_num) or (x==x_num and z==z_num) or (y==y_num and z==z_num) or (x==0 and y==0) or (x==x_num and y==0) or (x==0 and y==y_num) or (x==x_num and y==y_num):
            return 2
        else:
            if z==0 or z==z_num or y==0 or y==y_num or x==0 or x==x_num:
                return 4
            else:
                return 8
file_path_1= r"C:\Users\袁瑞浩\Desktop\Grid_E.out"
prev_line=None
with open (file_path_1,'r',encoding='utf-8') as file:
    for line in file:
        line=line.strip()
        if line == 'SPECGRID':
            prev_line='SPECGRID'
            continue
        if prev_line=='SPECGRID':
            line=line.split()
            x_num=int(line[0])
            y_num=int(line[1])
            z_num=int(line[2])
            break
##上述代码把任意给的网格模型i,j,k方向上网格数量统计出来了，分别放在x_num,y_num,z_num变量里面
###############################################################################################################
####以下代码负责把深度值放在depth_data这个列表里面
with open(file_path_1, 'r',encoding='utf-8') as file:
    found_zcorn = False  # 标记是否找到了 ZCORN 行
    depth_data = []  # 用于存储所有 ZCORN 数据
    for line in file:
        line = line.strip()  # 去除行尾的空白字符和换行符           
        # 检查当前行是否为 ZCORN
        if 'ZCORN' in line:
            found_zcorn = True
            continue  # 跳过 ZCORN 行，直接读取下一行                
        # 如果找到了 ZCORN，开始收集数据,即以下代码
        if found_zcorn:
            # 将当前行拆分成多个数据项，并加入 data 列表
            depth_data.extend(line.split())  # 默认按空格拆分               
            # 检查是否已经收集到足够的数据
            if len(depth_data) == 8*x_num*y_num*z_num:
                break
##############################################################################################################
x_list=[]
y_list=[]
point=[]
count=0
y_count=0
num1=0
num2=0
with open(file_path_1, 'r',encoding='utf-8') as file:
    count_line=0                #计数器，记录多少行，一般来说COORD关键字下有(x_num+1)*(y_num+1)行数据
    found_coord = False         # 标记是否找到了 COORD 行
    for line in file:
        line = line.strip()     # 去除行尾的空白字符和换行符          
        if 'COORD' in line:     # 检查当前行是否为 COORD
            found_coord = True
            continue            # 跳过这一行，直接读取下一行             
        if found_coord:         # 如果找到了 COORD，接下来读取它的数据,而只需要每一行前两个数据就行，即x坐标和y坐标
            count_line+=1
            if count_line<=(x_num+1)*(y_num+1):##count_line用来统计行数，正常来说ZCORN下面有(x_num+1)*(y_num+1)个数据
                data_items = line.split()  # 默认按空格拆分 此时data_items的格式是""0","0","-800","0","0","-200"",我只需要这个列表前两个数据                    
                x_list.append(data_items[0])
                y_list.append(data_items[1])
            if count_line==((x_num+1)*(y_num+1)):
                break
for k in range(z_num+1):
    for j in range(y_num+1):
        for i in range(j*(x_num+1),(j+1)*(x_num+1)):
            row=[]
            num1=count_shared_cubes((i-j*(x_num+1)),j,k)
            num2=num1+num2
            row.append(x_list[i])
            row.append(y_list[i])
            row.append(depth_data[num2-1])
            point.append(row)

#############################################################################################################################
## 以下代码负责把point里的列表元素化成浮点型，存储在excel表格里
import pandas as pd

# 将 point 列表中的每个元素转换为浮点型
point_float = [[float(item) for item in sublist] for sublist in point]

# 将 point_float 列表转换为 DataFrame
df = pd.DataFrame(point_float, columns=['X', 'Y', 'Depth'])

# 写入 Excel 文件
output_file_path = r"D:\原子大赛\Grid_E_change.xlsx"
df.to_excel(output_file_path, index=False)

print(f"Data has been written to {output_file_path}")   


   